package com.example.springboottest.studentscore.listener;

import com.example.springboottest.constant.Constant;
import com.example.springboottest.student.event.StuEvent;
import com.example.springboottest.studentscore.model.StuScore;
import com.example.springboottest.studentscore.service.StuScoreService;
import com.example.springboottest.util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

@Service
public class StuCreateEventListener implements ApplicationListener<StuEvent> {
    @Autowired
    private StuScoreService stuScoreService;

    @Override
    public void onApplicationEvent(StuEvent event) {
        String stuId = (String) event.getStuId();
        String eventType = (String) event.getEventType();
        if (StringUtils.isNoneBlank(stuId) && StringUtils.equals(eventType, Constant.EVENT_TYPE_CREATE)) {
            //默认给科目01和02分数是80
            StuScore stuScore1 = new StuScore(IdUtil.getUUID(), stuId, "01", 80);
            StuScore stuScore2 = new StuScore(IdUtil.getUUID(), stuId, "02", 80);
            stuScoreService.doCreate(stuScore1);
            stuScoreService.doCreate(stuScore2);
        }
    }
}
